﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 平板扫描仪
/// </summary>
public class ScanTablette : MonoBehaviour
{
    Vector3 mouse_Screen;
    Vector3 mouse_World;
    Vector3 target_Screen;
    Vector3 target_World;
    Vector3 offset;

    private void Start()
    {
        target_Screen = Camera.main.WorldToScreenPoint(transform.position);
    }

    private void OnMouseDown()
    {
        mouse_Screen = new Vector3(Input.mousePosition.x, Input.mousePosition.y, target_Screen.z);
        mouse_World = Camera.main.ScreenToWorldPoint(mouse_Screen);
        offset = transform.position - mouse_World;
    }

    private void OnMouseDrag()
    {
        mouse_Screen = new Vector3(Input.mousePosition.x, Input.mousePosition.y, target_Screen.z);
        mouse_World = Camera.main.ScreenToWorldPoint(mouse_Screen);
        target_World = new Vector3(transform.position.x, mouse_World.y + offset.y, mouse_World.z + offset.z);

        // 范围限制
        if (target_World.z <= -401f)
            target_World.z = -401f;
        if (target_World.z >= -399f)
            target_World.z = -399f;
        if (target_World.y >= 3.1f)
            target_World.y = 3.1f;
        if (target_World.y <= -1.4f)
            target_World.y = -1.4f;
        transform.position = target_World;
    }
}
